package com.lipy.java8study.map;

import java.util.HashMap;

/**
 * @description:
 * @author: lipy
 * @create: 2020-07-07 18:10
 **/

public class MapStudy {
    public static void main(String[] args) {
        compute();
    }

    private static void compute(){

            String str = "hello java, i am vary happy! nice to meet you";

            // jdk1.8的写法
            HashMap<Character, Integer> result2;
            result2 = new HashMap<>(32);
            for (int i = 0; i < str.length(); i++) {
                char curChar = str.charAt(i);
                result2.compute(curChar, (k,v) -> {
                    if(v == null){
                        v = 1;
                    }else{
                        v ++ ;
                    }
//                    System.out.println("k = " + k + " v = "+ v);
                    return v;
                });

            }
            result2.forEach((k,v) -> {
                System.out.println("k = " + k+" v = " + v);
            });

    }
    private static void merge(){
        String k = "key";
        HashMap<String, Integer> map = new HashMap<String, Integer>() {{
            put(k, 1);
        }};
        map.merge(k, 2, (oldVal, newVal) -> oldVal + newVal);
    }
}
